<?php
/**
 * Created by PhpStorm.
 * User: xuechunxuan
 * Date: 2018/10/18
 * Time: 11:04
 */
namespace app\index\controller;

use think\Request;

class Pay extends Session{
    public function getPay(){
        return new \app\index\model\Pay();
    }
    /*
     * 用户提现
     * 优先使用下级奖励金额
     * */
    public function userPay(Request $request){
        if($request->isGet()){
            $payArr=$this->getPay()->mySelectByKVPage(['user_id'=>\think\Session::get("uid"),'status'=>2]);
            $this->assign("payArr",$payArr);
          return  $this->fetch();
        }else if($request->isPost()){
            //发起提现
            $payAmount=$request->param("payAmount")*100;
            if(is_numeric($payAmount)){
                $userAmount=$this->userInfo['amount']+$this->userInfo['fall_into_amount'];
              //进行金额验证分为单位
                $H=date("H",time());
                if($H>19||$H<8){
                    $data=['code'=>305,'msg'=>"提现时间为早上8点至晚上8点"];
                }elseif($payAmount<1000||$payAmount>2000000){
                    $data=['code'=>301,'msg'=>"提现金额必须大于10元小于20000"];
                }elseif($payAmount%10!=0){
                    $data=['code'=>302,'msg'=>"提现金额必须为10的倍数"];
                }elseif(round((time()-$this->userInfo['reg_time'])/3600)<24){
                    $data=['code'=>308,'msg'=>"新用户注册24小时之内无法提现"];
                }elseif($this->userInfo['is_spread']!=1&&!$this->vilidateUser()){
                        //非推广账号做刷量判定
                        $data = ['code' => 310, 'msg' => "用户数据异常(刷量互关)拒绝提现"];
                }elseif($this->getPay()->getUserUnPay(\think\Session::get("uid"))){
                    $data=['code'=>304,'msg'=>"当前还有未支付提现申请"];
                }elseif($this->getPay()->getUserToDayPay(\think\Session::get("uid"))){
                    $data=['code'=>303,'msg'=>'每天只能提现一次'];
                }elseif($payAmount>$userAmount){
                    $data=['code'=>500,'msg'=>'可提现余额不足'];
                }else{

                     //创建支付数组
                    $data=[
                        'user_id'=>\think\Session::get("uid"),
                        'open_id'=>$this->userInfo['openid'],
                        'amount'=>$payAmount,
                        'start_time'=>time(),//发起提现时间
                        'status'=>1
                    ];
                    //优先使用分成金额进行支付并标记不成金额
                    $data['no_fall_into_amount']=$payAmount<$this->userInfo['fall_into_amount'] ?
                        $payAmount : $this->userInfo['fall_into_amount'];

                    //添加提现记录到数据库
                    if($payId=$this->getPay()->add($data)){
                        $data['id']=$payId;
                        if($payAmount<4000 ){
                            //直接调用支付接口
                            $data=['code'=>200,'msg'=>$this->getPay()->payWeiXinTX($payId)];
                        }else{
                            //提醒用户支付成功
                            $data=['code'=>200,'msg'=>'等待系统支付中,请留意微信支付到账通知'];
                        }
                    }else{
                        $data=['code'=>304,'msg'=>'发起支付失败,请重试'];
                    };

                }
                return json_encode($data);
            }
        }
    }
    /*
     * 用户提现明细
     * */
    public function userPayDetail(Request $request){
        if($request->isPost()){
            $page=$request->param("page") ? $request->param("page") : 0;
             echo json_encode($this->getPay()->mySelectByKVPage(['user_id'=>\think\Session::get("uid")],$page,10));
        }
    }


    /*
     * 刷量验证
     * */
    public function vilidateUser(){
      $userShareView=$this->getPay()->getUserShareViewByTime($this->userInfo['openid']);
      $num=round((time()-$this->userInfo['reg_time'])/86400);//每天起码1条数据
      return count($userShareView)>4+$num ? true :false;
    }



}